package com.symria.server.http.bean;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


public class StringUtil {
	public final static String NEWLINE = "\r";
	public final static String BLANK_SPACE = " ";
	private static final String BLANK = "";
	public static boolean isEmpty(String s){
		return s == null || BLANK.compareTo(s) == 0;
    }
	public static boolean isEmpty(Object str) {
		return str == null || BLANK.equals(str);
	}
	public static int getInt(String str, int defaultVal){
		if(str!=null){
			return Integer.valueOf(str);
		}
		return defaultVal;
	}
	public static Integer getInteger(Object str, Integer defaultVal){
		if(str!=null){
			return Integer.parseInt(str.toString());
		}
		
		return defaultVal;
	}
	public static String dealNull(String str) {
		return str != null ? str.trim() : BLANK;
	}
	public static String dealNull(Object obj) {
		String str = BLANK;
		if(obj!=null){
			if(obj instanceof String){
				str = (String)obj;
			}else{
				str = obj.toString();
			}
		}
		return str;
	}

	
	public static String dealNull(Object str,String defaultValue){
		return str != null ? str.toString().trim() : defaultValue;
	}
	
	public static String dealNull(String str, String defaultVal) {
		return str != null ? str.trim() : defaultVal;
	}
	public static String encode(String str, final String encoding) {
		try {
			if (!StringUtil.isEmpty(str)) {
				str = URLEncoder.encode(str, encoding);
			}
		} catch (Exception e) {
		}
		return str;
	}
	public static boolean equals(Object value1, Object value2) {
		boolean is = false;
		if (value1 == value2) { // is null or self
			return is = true;
		}
		if (value1 != null && value2 != null) { // is not null;
			return value1.equals(value2);
		}
		return is;
	}
	public static boolean regex(String regex, String str) {
		boolean trueOrFalse;
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		trueOrFalse = m.matches();
		return trueOrFalse;
	}
	public static String trim(String str){
		return StringUtil.dealNull(str).trim();
	}
	public static String subString(String str,int i){
		if(StringUtil.isEmpty(str)) return null;
		return str.length()>i?str.substring(0,i)+"...":str;
	}
	public static int random(int n){
		if(n==0) return 0;
		Random random = new Random();
		return Math.abs(random.nextInt())%n;
	}


    public static String filterIp(String ip){
    	if(StringUtil.isEmpty(ip)) return "";
    	String[][] object={new String[]{"\\.\\d*\\.",".*."},new String[]{"\\.\\d*\\.",".*."}}; 
    	return replace(ip,object);
    }
	public static String replace(final String sourceString,Object[] object) { 
        String temp=sourceString;    
        for(int i=0;i<object.length;i++){ 
           String[] result=(String[])object[i]; 
           Pattern    pattern = Pattern.compile(result[0]); 
           Matcher matcher = pattern.matcher(temp); 
           temp=matcher.replaceAll(result[1]); 
        } 
        return temp; 
    } 
	public static String fillZero(String code,Integer len){
    	String s = StringUtil.dealNull(code);//code.toString();
        while(s.length()<len){
        	s="0"+s;
        }
        return s;
    }
	public static String postFromURL(String url, NameValuePair[] msg)  {
		String strURL = url;
		String result = null;
		PostMethod post = null;
		try {
			post = new PostMethod(strURL);
			post.setRequestBody(msg);
			HttpClient httpclient = new HttpClient();
			httpclient.executeMethod(post);
			if (post.getStatusCode() == HttpStatus.SC_OK) {
				result = post.getResponseBodyAsString();
			} else {
				result = post.getStatusLine().toString();
			}
		} catch (Exception e) {
			//logger.logError("postFromURL Error!" + e.getMessage());
			e.printStackTrace();
		} finally {
			post.releaseConnection();
		}
		return result;
	}
	
	public static void main(String[] args){
		//String s = "D:"+File.separator+"Tomcat 6.0"+File.separator+"market"+File.separator+"ROOT"+File.separator+"userfiles"+File.separator+"app"+File.separator+"382";
		//String m = "D:\\Tomcat 6.0\\market\\ROOT\\userfiles\\app";
		//File file = new File(s);
		//String str = "?pgSize=20&pgFrom=10&titSize=117&cateCode=002003";
		//System.out.println(str.replaceAll("pgFrom=\\d*", "pgFrom=30"));
		//System.out.println(new MD5Util().getMD5ofStr("000000"));
		//String s="{KEY:\"REQ_PAYMENT\",CP_KEY:\"21031201\",PAY_INFO:\"[\"abc\",\"def\"]\"}";
		
		//System.out.println(EscapeUtil.escape(s));
		NameValuePair n = new NameValuePair();
		n.setName("p");
		n.setValue("%7B%22VER%22%3A%221.0%22%2C%22CP_KEY%22%3A%2221031201%22%2C%22KEY%22%3A%22QUERY_PAY_TYPE%22%2C%22PACKAGE_NAME%22%3A%22com.mine.sdk.pay%22%2C%22CHANNEL_ID%22%3A%22null%22%2C%22EXT_INFO%22%3A%5B%22867746013177670%22%2C%22460021169315777%22%2C%22MI%201S%22%2C%2215%22%2C%221%22%5D%7D");
		System.out.println(StringUtil.postFromURL("http://service.cool2line.com:8090/pay", new NameValuePair[]{n}));
	}
}
